<?php
/**
 * Created by PhpStorm.
 * User: lh
 * Date: 2020/1/20
 * Time: 11:55
 */

namespace App\Services\Com;


use App\Models\Activity\Activity;
use App\Models\Group\Group;
use App\Models\Member\Comment;
use App\Models\Member\Member;
use App\Models\Posts\Posts;
use App\Models\Store\Store;
use App\Models\Vote\Vote;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;

class StateService
{

    public static $serviceTypeMaps = [
            0 => 'comment',
            2 => 'vote',
            3 => 'activity',
            4 => 'store',
            5 => 'feed',
            6 => 'user',
            7 => 'talking',
        ];

    public static function getStanceModel($serviceType = 'talking')
    {

        $modelMaps = [
            'comment'  => new Comment(),
            'talking'  => new Group(),
            'store'    => new Store(),
            'feed'     => new Posts(),
            'activity' => new Activity(),
            'user'     => new Member(),
            'vote'     => new Vote(),
        ];
        if (isset($modelMaps[$serviceType])) {
            return $modelMaps[$serviceType];
        }
        return Group::query();
    }

    public static function getServiceUuidInfo(int $place = -1, $uuid = '')
    {
        $res = [];
        if(isset(self::$serviceTypeMaps[$place])){
            $res = self::getStanceModel(self::$serviceTypeMaps[$place])->query()
            ->where('uuid', $uuid)
            ->first();
        }
        return $res;
    }

    public static function checkServiceUuid($serviceType = '', $uuid = '')
    {
        if (empty($uuid) && empty($serviceType)) {
            return true;
        }
        //talking的可以是空
        if (empty($uuid) && $serviceType == 'talking') {
            return true;
        }
        return self::getStanceModel($serviceType)->query()
            ->where('uuid', $uuid)
            ->first();
    }

    public static function eventSendMessage($requestParams = [])
    {
        $place                = $requestParams['place'];
        $uuid                 = $requestParams['service_uuid']??'未知';
        $serviceTypeMaps      = self::$serviceTypeMaps;
        
        $sendMember = self::checkServiceUuid($serviceTypeMaps[$place] ?? 'talking', $uuid);
        if (!$sendMember) {
            Log::info('空原文');
            return null;
        }else{
            switch ($serviceTypeMaps[$place]) {
                case 'activity':
                case 'talking':
                    $all_context = $sendMember->name;
                    break;
                case 'vote':
                case 'store':
                    $all_context = $sendMember->title;
                    break;
                case 'feed':
                    $all_context = !empty($sendMember->content) ? $sendMember->content : '推文';
                    break;
                default: //feed user comment
                    $all_context = !empty($sendMember->content) ? $sendMember->content : $sendMember->desc;
                    break;
            }
            
        }

        //自己发的不用发通知
        if ($sendMember->member_uuid == Auth::user()->uuid) {
            Log::info('自己发的不用发通知');
            return null;
        }
        $sendParams = [
            'type'         => $requestParams['type'],
            'service_uuid' => $uuid,
            'all_context'  => $all_context,
            'route_link'   => $requestParams['route'] ?? '',
            'place'        => $place,
        ];
        $memberUuid = $sendMember->member_uuid;
        if(!empty($requestParams['myself_uuid'])){ // 自己发给自己
            $memberUuid = Auth::user()->uuid;
        }
        MessageService::sendMessage($sendParams, [$memberUuid], Auth::user());
    }
}